﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Jy123.Web.AppCode;
using Jy123.Common;

namespace Jy123.Web.Manage
{
    public partial class Maintain : MgConfig
    {
        private List<Jy123.Model.Public_JType> listJType;
        private Dictionary<int, string> JTypeDic = new Dictionary<int, string>();
        protected void Page_Load(object sender, EventArgs e)
        {
            Verify("Maintain.aspx");
            string strWhere = "JFather = '1' OR JFather = '3'";
            Jy123.BLL.Public_JType BLLJType = new Jy123.BLL.Public_JType();
            listJType = BLLJType.GetModelList(strWhere);
            foreach (Jy123.Model.Public_JType JType in listJType)
            {
                JTypeDic.Add(JType.ID, JType.JName);
            }
            if (!Page.IsPostBack)
            {
                LoadInitialValue();
            }
        }

        protected void Repeater_DataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Jy123.Model.Public_Maintain maintain = e.Item.DataItem as Jy123.Model.Public_Maintain;
                HiddenField idHidden = e.Item.FindControl("idHidden") as HiddenField;
                idHidden.Value = maintain.ID.ToString();
                Label nameLabel = e.Item.FindControl("maintainNameLabel") as Label;
                Label addPointLabel = e.Item.FindControl("addPointLabel") as Label;
                Label costLabel = e.Item.FindControl("costLabel") as Label;
                Label JTypeLabel = e.Item.FindControl("JTypeLabel") as Label;
                nameLabel.Text = maintain.MName;
                addPointLabel.Text = maintain.AddPoint.Value.ToString();
                costLabel.Text = maintain.Cost.Value.ToString();
                JTypeLabel.Text = JTypeDic[maintain.FJType.Value];
            }
        }

        protected void Repeater_Command(object sender, RepeaterCommandEventArgs e)
        {
            RepeaterItem item = e.Item as RepeaterItem;
            HiddenField hidden = e.Item.FindControl("idHidden") as HiddenField;
            int id = int.Parse(hidden.Value);
            Jy123.BLL.Public_Maintain BLLMaintain = new Jy123.BLL.Public_Maintain();
            if (e.CommandName.Equals("Edit"))
            {
                Jy123.Model.Public_Maintain maintain = BLLMaintain.GetModel(id);
                actionTypeHidden.Value = "Update";
                idHidden.Value = id.ToString();
                maintainNameTextBox.Text = maintain.MName;
                addPointTextBox.Text = maintain.AddPoint.Value.ToString();                
                costTextBox.Text = maintain.Cost.Value.ToString();
                if (maintain.FJType.HasValue)
                {
                    JTypeDropDown.SelectedValue = maintain.FJType.Value.ToString();
                }
                iconImg.Src = maintain.Model;
            }
            else if (e.CommandName.Equals("Delete"))
            {
                BLLMaintain.Delete(id);
                LoadInitialValue();
                ClearPage();
            }
        }

        protected void Save(object sender, EventArgs e)
        {
            Jy123.BLL.Public_Maintain BLLMaintain = new Jy123.BLL.Public_Maintain();
            if (actionTypeHidden.Value.Equals("Add"))
            {
                Jy123.Model.Public_Maintain maintain = new Jy123.Model.Public_Maintain();
                SetValue(maintain);
                maintain.ID = BLLMaintain.Add(maintain);
                if (fileLogo.PostedFile != null)
                {
                    maintain.Model = CreateLogo(maintain.ID);
                }
                BLLMaintain.Update(maintain);
            }
            else
            {
                int id = int.Parse(idHidden.Value);
                Jy123.Model.Public_Maintain maintain = BLLMaintain.GetModel(id);
                SetValue(maintain);
                if (fileLogo.PostedFile != null)
                {
                    string strLogo = CreateLogo(id);
                    if (!string.IsNullOrEmpty(strLogo))
                    {
                        maintain.Model = strLogo;
                    }
                }
                BLLMaintain.Update(maintain);
            }
            LoadInitialValue();
            ClearPage();
        }

        private void ClearPage()
        {
            actionTypeHidden.Value = "Add";
            maintainNameTextBox.Text = string.Empty;
            addPointTextBox.Text = string.Empty;
            costTextBox.Text = string.Empty;
        }

        private void SetValue(Jy123.Model.Public_Maintain maintain)
        {
            maintain.MName = maintainNameTextBox.Text;
            maintain.FJType = int.Parse(JTypeDropDown.SelectedValue);
            maintain.AddPoint = int.Parse(addPointTextBox.Text);
            maintain.Cost = int.Parse(costTextBox.Text);
        }

        private void LoadInitialValue()
        {
            Jy123.BLL.Public_Maintain BLLMaintain = new Jy123.BLL.Public_Maintain();
            List<Jy123.Model.Public_Maintain> listMaintain = BLLMaintain.GetModelList(string.Empty);
            maintainRepeater.DataSource = listMaintain;
            maintainRepeater.DataBind();
            maintainRepeater.Visible = listMaintain.Count > 0;
            BindDropDown(listJType);
        }

        private void BindDropDown(List<Jy123.Model.Public_JType> listJType)
        {
            JTypeDropDown.Items.Clear();
            JTypeDropDown.DataSource = listJType;
            JTypeDropDown.DataTextField = "JName";
            JTypeDropDown.DataValueField = "ID";
            JTypeDropDown.DataBind();
            JTypeDropDown.Items.Insert(0, new ListItem("请选择", "-1"));
        }


        protected string CreateLogo(int id)
        {
            CreateDir createDir = new CreateDir();
            HttpPostedFile postFile = fileLogo.PostedFile;
            string logoPath = postFile.FileName;
            string logoName = string.Empty;
            if (!string.IsNullOrEmpty(logoPath))
            {
                string logoType = logoPath.Substring(logoPath.LastIndexOf(".") + 1);
                logoName = createDir.CreateMaintainPath(CreateDir.MAINTAIN, id, logoType);
                string mapPath = Server.MapPath("/");

                Upload upload = new Upload();
                upload.PostedFile = postFile;
                upload.FileLength = postFile.ContentLength;
                upload.FileName = logoName;
                upload.Extension = logoType;
                upload.SavePath = mapPath;
                upload.UploadStart(logoName);
            }
            return logoName;
        }
    }
}
